package com.laoniu.service.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoniu.dto.request.LogRequestDto;
import com.laoniu.entity.SecurityUser;
import com.laoniu.entity.SysLog;
import com.laoniu.mapper.SysLogMapper;
import com.laoniu.utils.ServletUtils;
import com.laoniu.utils.SpringSecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class LogService {

    @Resource
    private SysLogMapper logMapper;

    Logger log = LoggerFactory.getLogger(LogService.class);
    /**
     * 查询日志列表
     * @return
     */
    public List<SysLog> list(){

        LambdaQueryWrapper<SysLog> wrapper = Wrappers.lambdaQuery(SysLog.class);
        wrapper.orderByDesc(SysLog::getId);
        SecurityUser user = SpringSecurityUtils.getUser();
        if (!user.isAdmin()){
            wrapper.eq(SysLog::getOperateUserId, user.getId());
        }
        return logMapper.selectList(wrapper);
    }

    /**
     * 添加日志
     * @param bizName
     * @param info
     * @return
     */
    public int insertLog(String bizName, String info, String operateType, String operateDesc){
        SysLog sysLog = buildLog();
        sysLog.setBizName(bizName);
        sysLog.setInfo(info);
        sysLog.setOperateType(operateType);
        sysLog.setOperateDesc(operateDesc);
        log.info("添加日志：{}", JSONObject.toJSONString(sysLog));
        return logMapper.insert(sysLog);
    }

    /**
     * 构建日志实体
     * @return
     */
    private SysLog buildLog(){
        SysLog sysLog = new SysLog();
        //获取设备ip
        sysLog.setRequestIp(ServletUtils.getRequestIp());
        //获取设备信息
        sysLog.setRequestDevice(ServletUtils.getDevice());
        //创建时间
        sysLog.setCreateTime(LocalDateTime.now());
        //请求地址
        sysLog.setUrl(ServletUtils.getUrl());
        //获取登录用户
        SecurityUser user = SpringSecurityUtils.getUser();
        if (Objects.nonNull(user)){
            sysLog.setOperateUser(user.getName());
            sysLog.setOperateUserId(user.getId());
        }
        return sysLog;
    }

    public int insertLogAddUser(SecurityUser user,String bizName, String info,String url, String operateType, String operateDesc){
        SysLog sysLog = new SysLog();
        //获取设备ip
        sysLog.setRequestIp(ServletUtils.getRequestIp());
        //获取设备信息
        sysLog.setRequestDevice(ServletUtils.getDevice());
        //创建时间
        sysLog.setCreateTime(LocalDateTime.now());
        if (Objects.nonNull(user)){
            sysLog.setOperateUser(user.getName());
            sysLog.setOperateUserId(user.getId());
        }
        sysLog.setUrl(ServletUtils.getUrl());
        sysLog.setInfo(info);
        sysLog.setBizName(bizName);
        sysLog.setOperateType(operateType);
        sysLog.setOperateDesc(operateDesc);
        log.info("添加日志：{}", JSONObject.toJSONString(sysLog));
        return logMapper.insert(sysLog);
    }

    public Page<SysLog> pageList(LogRequestDto requestDto) {
        Page<SysLog> page = new Page<>(requestDto.getPageNo(), requestDto.getPageSize());
        LambdaQueryWrapper<SysLog> wrapper = Wrappers.lambdaQuery(SysLog.class);
        SecurityUser user = SpringSecurityUtils.getUser();
        if (!user.isAdmin()){
            wrapper.eq(SysLog::getOperateUserId, user.getId());
        }
        wrapper.like(StringUtils.isNotBlank(requestDto.getBizName()), SysLog::getBizName, requestDto.getBizName());
        wrapper.like(StringUtils.isNotBlank(requestDto.getRequestIp()), SysLog::getRequestIp, requestDto.getRequestIp());
        wrapper.like(StringUtils.isNotBlank(requestDto.getUrl()), SysLog::getUrl, requestDto.getUrl());
        wrapper.like(StringUtils.isNotBlank(requestDto.getUsername()), SysLog::getOperateUser, requestDto.getUsername());
        wrapper.orderByDesc(SysLog::getId);
        return logMapper.selectPage(page, wrapper);
    }
}
